package repository;

import fileoperation.FileCreation;

import java.io.File;
import java.io.IOException;

/**
 * 任务一~~~~~~~~~~
 * 添加自己的代码，JitInit.init("worktree")应该能够创建一个存储库“worktree/.jit”
 * 其中包含所有默认文件和其他的存储库。如果存储库已存在，请删除前一个并创建新的，欢迎你重构代码，但是不要
 * 修改核心目录中的代码
 */
public class Repository {//仓库类
    private static String workTree;	//working directory 工作目录
    private static String gitDir;	//jit repository path jit仓库路径
    private static String indexLoc;//jit 文件夹中index文件的位置
    private static String commitHistory;

    /**
     * Constructor
     */
    public Repository() throws IOException {
        if(gitDir == ""){//如果jit仓库路径为空，说明该仓库不存在
            throw new IOException("The repository does not exist!");
        }
    }
    
    /**
     * Construct a new repository instance with certain path.
     * 在指定目录新建一个仓库实例
     * Constructor
     * @param path
     * @throws IOException
     */
    public Repository(String path) throws IOException {
        this.workTree = path;
        this.gitDir = path + File.separator + ".jit";
        this.indexLoc=path+File.separator+".jit"+File.separator+"index";
        this.commitHistory=path+File.separator+".jit"+File.separator+"logs"+File.separator+"refs"+File.separator+"heads"+File.separator+"master";
    }

    public static String getGitDir() {
        return gitDir;
    }
    public static String getWorkTree() {
        return workTree;
    }
    public static String getIndexLoc(){return indexLoc;}
    public static String getCommitHistory(){return commitHistory;}
    
    /**
     * Helper functions.
     * 其他辅助功能
     * @return
     */
    public boolean exist(){ return new File(gitDir).exists(); }

    public boolean isFile(){ return new File(gitDir).isFile(); }

    public boolean isDirectory(){ return new File(gitDir).isDirectory(); }


    /**
     * Create the repository and files and directories inside.
     * 创建一个新的仓库，包含其中的文件和文件夹
     * @return boolean
     * @throws IOException
     */
    public void createRepo() throws Exception {
        //1.首先创建一些文件和文件夹
        FileCreation.createDirectory(workTree,".jit");
        FileCreation.createDirectory(gitDir,"hooks");
        FileCreation.createDirectory(gitDir,"logs","refs");
        FileCreation.createDirectory(gitDir,"logs","refs","heads");
        FileCreation.createDirectory(gitDir,"objects","info");
        FileCreation.createDirectory(gitDir,"objects","pack");
        FileCreation.createDirectory(gitDir,"refs","heads");
        FileCreation.createDirectory(gitDir,"refs","tags");
        FileCreation.createDirectory(gitDir,"info");
        FileCreation.createDirectory(gitDir,"branches");
        FileCreation.createFile(gitDir,"index","");
        FileCreation.createFile(gitDir,"HEAD","ref: refs/heads/master\n");
        FileCreation.createFile(gitDir,"config","");
        FileCreation.createFile(gitDir,"description","");
        FileCreation.createFile(gitDir+File.separator+"logs","HEAD","");
        FileCreation.createFile(gitDir+File.separator+"logs"+File.separator+"refs"+File.separator+"heads","master","");
        FileCreation.createFile(gitDir+File.separator+"refs"+File.separator+"heads","master","");
        //2.使用serFiles()递归地遍历整个文件夹,将工作区中的所有文件序列化到本地仓库
        //addUtils.serFiles(workTree);

    }

//        for(int i=0;i<fs.length;i++){
//            if(fs[i].isDirectory()&&fs[i].getName().equals(".jit"))
//                continue;
//            if(fs[i].isFile()) {//如果是文件则直接序列化到本地仓库
//                try {
//                    blob = new Blob(fs[i]);
//                    blob.writeObject();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }else {//如果遍历到的是文件夹，则先序列化该文件夹下一层的文件，再对文件夹进行序列化
//                serFiles(filePath+File.separator+fs[i].getName());
//                try {
//                    tree=new Tree(fs[i]);
//                    tree.writeObject();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }
    }


